/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Utilities
*        文件名:             BaseBusinessLogic.cs
*        创建系统时间:       2012/11/2 19:07:07
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Utilities
{
    using System;
    using System.Collections;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Reflection;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;

    public class BaseBusinessLogic
    {
        public static string FieldCategoryID = "CategoryID";
        public static string FieldCode = "Code";
        public static string FieldCreateDate = "CreateDate";
        public static string FieldCreateUserID = "CreateUserID";
        public static string FieldDeleteMark = "DeleteMark";
        public static string FieldEnabled = "Enabled";
        public static string FieldFullName = "FullName";
        public static string FieldID = "ID";
        public static string FieldModifyDate = "ModifyDate";
        public static string FieldModifyUserID = "ModifyUserID";
        public static string FieldParentID = "ParentID";
        public static string FieldSortCode = "SortCode";
        public static string SelectedColumn = "SELECTED";
        public static string SQLLogicConditional = " AND ";

        public static Image ByteToImage(byte[] buffer)
        {
            MemoryStream stream = new MemoryStream();
            stream = new MemoryStream(buffer);
            Image image = Image.FromStream(stream);
            stream.Close();
            return image;
        }

        public static string[] Concat(params string[][] ids)
        {
            Hashtable hashtable = new Hashtable();
            if (ids != null)
            {
                for (int j = 0; j < ids.Length; j++)
                {
                    if (ids[j] != null)
                    {
                        for (int k = 0; k < ids[j].Length; k++)
                        {
                            if (!hashtable.ContainsKey(ids[j][k]))
                            {
                                hashtable.Add(ids[j][k], ids[j][k]);
                            }
                        }
                    }
                }
            }
            string[] strArray = new string[hashtable.Count];
            IDictionaryEnumerator enumerator = hashtable.GetEnumerator();
            for (int i = 0; enumerator.MoveNext(); i++)
            {
                strArray[i] = enumerator.Key.ToString();
            }
            return strArray;
        }

        public static bool ConvertIntToBoolean(object Object)
        {
            if (Object == DBNull.Value)
            {
                return false;
            }
            if (!Object.ToString().Equals("1"))
            {
                bool flag = true;
                return Object.ToString().Equals(flag.ToString());
            }
            return true;
        }

        public static DateTime ConvertToDateTime(object Object)
        {
            if (Object == DBNull.Value)
            {
                return DateTime.Now;
            }
            return Convert.ToDateTime(Object);
        }

        public static string ConvertToDateToString(object Object)
        {
            return ((Object != DBNull.Value) ? DateTime.Parse(Object.ToString()).ToString(BaseSystemInfo.DateFormat) : null);
        }

        public static decimal ConvertToDecimal(object Object)
        {
            if (Object == DBNull.Value)
            {
                return 0M;
            }
            return Convert.ToDecimal(Object);
        }

        public static int ConvertToInt(object Object)
        {
            if (Object == DBNull.Value)
            {
                return 0;
            }
            return Convert.ToInt32(Object);
        }

        public static long ConvertToInt64(object Object)
        {
            if (Object == DBNull.Value)
            {
                return 0L;
            }
            return Convert.ToInt64(Object);
        }

        public static string ConvertToString(object Object)
        {
            if (Object == DBNull.Value)
            {
                return null;
            }
            return Object.ToString();
        }

        public static object CopyObjectProperties(object sourceObject, object targetObject)
        {
            Type type = sourceObject.GetType();
            Type type2 = targetObject.GetType();
            PropertyInfo[] properties = type.GetProperties();
            PropertyInfo[] infoArray2 = type2.GetProperties();
            for (int i = 0; i < infoArray2.Length; i++)
            {
                for (int j = 0; j < properties.Length; j++)
                {
                    if (infoArray2[i].Name.Equals(properties[j].Name))
                    {
                        if (infoArray2[i].CanWrite)
                        {
                            object obj2 = properties[j].GetValue(sourceObject, null);
                            infoArray2[i].SetValue(targetObject, obj2, null);
                        }
                        break;
                    }
                }
            }
            return targetObject;
        }

        public static object CopyObjectValue(object sourceObject, object targetObject)
        {
            string name = string.Empty;
            FieldInfo[] fields = sourceObject.GetType().GetFields(BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
            for (int i = 0; i < fields.Length; i++)
            {
                name = fields[i].Name;
                FieldInfo info = fields[i];
                SetClassValue(targetObject, name, info.GetValue(sourceObject));
            }
            return targetObject;
        }

        public static int Delete(DataTable dataTable, string id)
        {
            return Delete(dataTable, FieldID, id);
        }

        public static int Delete(DataTable dataTable, string fieldName, string fieldValue)
        {
            int num = 0;
            foreach (DataRow row in dataTable.Rows)
            {
                if (row[fieldName].ToString().Equals(fieldValue))
                {
                    row.Delete();
                    num++;
                    return num;
                }
            }
            return num;
        }

        public static int EndDebug(MethodBase methodBase, int milliStart)
        {
            int tickCount = Environment.TickCount;
            Console.WriteLine(DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds((double)(tickCount - milliStart)).ToString() + " :End: " + methodBase.ReflectedType.Name + "." + methodBase.Name);
            return (tickCount - milliStart);
        }

        public static bool Exists(string[] ids, string targetString)
        {
            for (int i = 0; i < ids.Length; i++)
            {
                if (ids[i].Equals(targetString))
                {
                    return true;
                }
            }
            return false;
        }

        public static bool Exists(DataTable dataTable, string fieldName, string fieldValue)
        {
            if (dataTable != null)
            {
                foreach (DataRow row in dataTable.Rows)
                {
                    if (row[fieldName].ToString().Equals(fieldValue))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public static string[] FieldToArray(DataTable dataTable, string name)
        {
            string[] strArray = new string[0];
            int num = 0;
            string str = string.Empty;
            foreach (DataRow row in dataTable.Rows)
            {
                num++;
                str = str + row[name].ToString() + ",";
            }
            if (num > 0)
            {
                strArray = str.Substring(0, str.Length - 1).Split(new char[] { ',' });
            }
            return strArray;
        }

        public static string FieldToList(DataTable dataTable)
        {
            return FieldToList(dataTable, FieldID);
        }

        public static string FieldToList(DataTable dataTable, string name)
        {
            int num = 0;
            string str = "'";
            foreach (DataRow row in dataTable.Rows)
            {
                num++;
                str = str + row[name].ToString() + "', '";
            }
            if (num == 0)
            {
                return "''";
            }
            return str.Substring(0, str.Length - 3);
        }

        public static byte[] GetBinaryFormatData(DataTable dataTable)
        {
            byte[] buffer = null;
            dataTable.RemotingFormat = SerializationFormat.Binary;
            MemoryStream serializationStream = new MemoryStream();
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(serializationStream, dataTable);
            buffer = serializationStream.ToArray();
            serializationStream.Close();
            serializationStream.Dispose();
            return buffer;
        }

        public static DataRow GetDataRow(DataTable dataTable, string id)
        {
            return GetDataRow(dataTable, FieldID, id);
        }

        public static DataRow GetDataRow(DataTable dataTable, string fieldName, string fieldValue)
        {
            foreach (DataRow row2 in dataTable.Rows)
            {
                if ((row2.RowState != DataRowState.Deleted) && row2[fieldName].ToString().Equals(fieldValue))
                {
                    return row2;
                }
            }
            return null;
        }

        public static string GetDateTime(DataRow dataRow, string name)
        {
            string str = string.Empty;
            if (!dataRow.IsNull(name))
            {
                str = DateTime.Parse(dataRow[name].ToString()).ToString(BaseSystemInfo.DateFormat);
            }
            return str;
        }

        public static string GetDayOfWeek(string dayOfWeek)
        {
            return GetDayOfWeek(dayOfWeek, false);
        }

        public static string GetDayOfWeek(string dayOfWeek, bool chinese)
        {
            string str = "0";
            switch (dayOfWeek)
            {
                case "Sunday":
                    str = "0";
                    break;

                case "Monday":
                    str = "1";
                    break;

                case "Tuesday":
                    str = "2";
                    break;

                case "Wednesday":
                    str = "3";
                    break;

                case "Thursday":
                    str = "4";
                    break;

                case "Friday":
                    str = "5";
                    break;

                case "Saturday":
                    str = "6";
                    break;
            }
            if (chinese)
            {
                switch (str)
                {
                    case "0":
                        return "星期日";

                    case "1":
                        return "星期一";

                    case "2":
                        return "星期二";

                    case "3":
                        return "星期三";

                    case "4":
                        return "星期四";

                    case "5":
                        return "星期五";

                    case "6":
                        return "星期六";
                }
            }
            return str;
        }

        public static byte[] GetFile(string fileName)
        {
            FileStream input = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            BinaryReader reader = new BinaryReader(input);
            byte[] buffer = reader.ReadBytes((int)input.Length);
            reader.Close();
            input.Close();
            return buffer;
        }

        public static string GetFriendlyFileSize(double fileSize)
        {
            if (fileSize < 1024.0)
            {
                return (fileSize.ToString("F1") + "Byte");
            }
            fileSize /= 1024.0;
            if (fileSize < 1024.0)
            {
                return (fileSize.ToString("F1") + "KB");
            }
            fileSize /= 1024.0;
            if (fileSize < 1024.0)
            {
                return (fileSize.ToString("F1") + "M");
            }
            fileSize /= 1024.0;
            return (fileSize.ToString("F1") + "GB");
        }

        public static PermissionScope GetPermissionScope(string[] organizeIDs)
        {
            PermissionScope none = PermissionScope.None;
            foreach (PermissionScope scope2 in (PermissionScope[])Enum.GetValues(typeof(PermissionScope)))
            {
                if (Exists(organizeIDs, scope2.ToString()))
                {
                    return scope2;
                }
            }
            return none;
        }

        public static string GetProperty(DataTable dataTable, string id, string targetField)
        {
            return GetProperty(dataTable, FieldID, id, targetField);
        }

        public static string GetProperty(DataTable dataTable, string fieldName, string fieldValue, string targetField)
        {
            string str = string.Empty;
            foreach (DataRow row in dataTable.Rows)
            {
                if (row[fieldName].ToString().Equals(fieldValue))
                {
                    return row[targetField].ToString();
                }
            }
            return str;
        }

        public static string GetSearchString(string search)
        {
            search = search.Trim();
            search = SqlSafe(search);
            if (search.Length > 0)
            {
                search = search.Replace('[', '_');
                search = search.Replace(']', '_');
            }
            if (search == "%")
            {
                search = "[%]";
            }
            if (((search.Length > 0) && (search.IndexOf('%') < 0)) && (search.IndexOf('_') < 0))
            {
                search = "%" + search + "%";
            }
            return search;
        }

        public static byte[] ImageToByte(Image Image)
        {
            MemoryStream stream = new MemoryStream();
            Image.Save(stream, ImageFormat.Gif);
            byte[] buffer = stream.GetBuffer();
            stream.Close();
            return buffer;
        }

        public static bool IsAuthorized(BaseUserInfo userInfo, string permissionCode)
        {
            return false;
        }

        public static bool IsAuthorized(DataTable dataTable, string permissionCode)
        {
            return Exists(dataTable, FieldCode, permissionCode);
        }

        public static bool IsAuthorized(DataTable dataTable, BaseUserInfo userInfo, string permissionCode)
        {
            return IsAuthorized(dataTable, userInfo.ID, permissionCode);
        }

        public static bool IsAuthorized(DataTable dataTable, string userID, string permissionCode)
        {
            return Exists(dataTable, FieldCode, permissionCode);
        }

        public static string ObjectsToList(object[] ids)
        {
            string str2 = "'";
            for (int i = 0; i < ids.Length; i++)
            {
                str2 = str2 + ids[i] + "', '";
            }
            if (ids.Length == 0)
            {
                return "''";
            }
            return str2.Substring(0, str2.Length - 3);
        }

        public static string[] Remove(string[] ids, string id)
        {
            Hashtable hashtable = new Hashtable();
            if (ids != null)
            {
                for (int j = 0; j < ids.Length; j++)
                {
                    if (((ids[j] != null) && !ids[j].Equals(id)) && !hashtable.ContainsKey(ids[j]))
                    {
                        hashtable.Add(ids[j], ids[j]);
                    }
                }
            }
            string[] strArray = new string[hashtable.Count];
            IDictionaryEnumerator enumerator = hashtable.GetEnumerator();
            for (int i = 0; enumerator.MoveNext(); i++)
            {
                strArray[i] = enumerator.Key.ToString();
            }
            return strArray;
        }

        public static string RepeatString(string targetString, int repeatCount)
        {
            string str = string.Empty;
            for (int i = 0; i < repeatCount; i++)
            {
                str = str + targetString;
            }
            return str;
        }

        public static DataTable RetrieveDataTable(byte[] ArrayResult)
        {
            MemoryStream serializationStream = new MemoryStream(ArrayResult);
            IFormatter formatter = new BinaryFormatter();
            object obj2 = formatter.Deserialize(serializationStream);
            serializationStream.Close();
            serializationStream.Dispose();
            return (DataTable)obj2;
        }

        public static void SaveFile(byte[] file, string fileName)
        {
            FileStream stream = new FileStream(fileName, FileMode.Create);
            stream.Write(file, 0, file.Length);
            stream.Close();
        }

        private static int SetClassValue(object targetObject, string name, object value)
        {
            int num = 0;
            Type type = targetObject.GetType();
            FieldInfo[] fields = type.GetFields(BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
            for (int i = 0; i < fields.Length; i++)
            {
                if (name.Equals(fields[i].Name))
                {
                    type.GetField(name).SetValue(targetObject, value);
                    num++;
                    return num;
                }
            }
            return num;
        }

        public static DataTable SetFilter(DataTable dataTable, string name, string value)
        {
            return SetFilter(dataTable, name, value, false);
        }

        public static DataTable SetFilter(DataTable dataTable, string name, string value, bool equals)
        {
            foreach (DataRow row in dataTable.Rows)
            {
                if (equals)
                {
                    if (row[name].ToString().Equals(value))
                    {
                        row.Delete();
                    }
                }
                else if (!row[name].ToString().Equals(value))
                {
                    row.Delete();
                }
            }
            dataTable.AcceptChanges();
            return dataTable;
        }

        public static int SetProperty(DataTable dataTable, string id, string targetField, object targetValue)
        {
            return SetProperty(dataTable, FieldID, id, targetField, targetValue);
        }

        public static int SetProperty(DataTable dataTable, string fieldName, string fieldValue, string targetField, object targetValue)
        {
            int num = 0;
            foreach (DataRow row in dataTable.Rows)
            {
                if ((row.RowState != DataRowState.Deleted) && row[fieldName].ToString().Equals(fieldValue))
                {
                    row[targetField] = targetValue;
                    num++;
                    return num;
                }
            }
            return num;
        }

        public static string SqlSafe(string value)
        {
            value = value.Replace("'", "''");
            return value;
        }

        public static int StartDebug(MethodBase methodBase)
        {
            Console.WriteLine(DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat) + " :Begin: " + methodBase.ReflectedType.Name + "." + methodBase.Name);
            return Environment.TickCount;
        }

        public static int StartDebug(BaseUserInfo userInfo, MethodBase methodBase)
        {
            Console.WriteLine(" User: " + userInfo.Realname + " IP: " + userInfo.IPAddress);
            Console.WriteLine(DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat) + " :Begin: " + methodBase.ReflectedType.Name + "." + methodBase.Name);
            return Environment.TickCount;
        }

        public static void WriteDebug(BaseUserInfo userInfo, MethodBase methodBase)
        {
            Console.WriteLine(DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat) + " " + userInfo.IPAddress + methodBase.ReflectedType.Name + "." + methodBase.Name);
        }

        private static BaseBusinessLogic _BaseBusinessLogic = new BaseBusinessLogic();

        public static BaseBusinessLogic get_Instance()
        {
            return _BaseBusinessLogic;
        }
    }
}